﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EzDesk.DevHelper.DebugLibrary.Helper
{
    /// <summary>
    /// 分组字符串生成的帮助类
    /// </summary>
    public static class GroupMemeberStrHelper
    {
        public static string GetGroupMemberStrFromList(IList<string> userIdList, IList<string> groupIdList)
        {
            List<string> memberStrList = new List<string>()
                                             {
                                                 ConvertListToString(userIdList, ","),
                                             };
            string groupListStr = ConvertListToString(groupIdList, ",");
            if (!String.IsNullOrEmpty(groupListStr))
            {
                memberStrList.Add(groupListStr);
            }
            return ConvertListToString(memberStrList, "|");
        }

        private static string ConvertListToString(IList<string> stringList, string seperator)
        {
            if (stringList == null || stringList.Count == 0)
            {
                return String.Empty;
            }
            if (stringList.Count == 1)
            {
                return stringList[0];
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < stringList.Count(); i++)
            {
                if (i != 0)
                {
                    sb.Append(seperator);
                }
                sb.Append(stringList[i]);
            }
            return sb.ToString();
        }

        /// <summary>
        /// 清理用户Id,去掉自己的Id，去掉重复Id
        /// </summary>
        /// <param name="senderIdStr"></param>
        public static string CleanUpUserIds(string userIdsStr, string senderIdStr)
        {
            var userIdList = userIdsStr.Split(',').Distinct().Select(
                p =>
                {
                    return new Guid(p);
                }
            ).ToList();
            if (!String.IsNullOrEmpty(senderIdStr))
            {
                Guid senderId = new Guid(senderIdStr);
                if (userIdList.Contains(senderId))
                {
                    userIdList.Remove(senderId);
                }
            }
            return GetGroupMemberStrFromList(
                userIdList.Select(p => { return p.ToString(); }).ToList(),
                null);
        }
    }
}
